<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>通过form表单提交</h1>
    <form id="myform" name="myform" method="post" enctype="multipart/form-data" action="http://localhost:5193/WeatherForecast/Upload/FormImg">
        <input type="file" name="files" id="files" value="选择需要上传的文件" multiple />
        <input type="button" id="submitbtn" value="提交" onclick="uplpadfile()">
    </form>

    <div>
        上传的图片，返回的地址
        <div id="imglist"></div>
    </div>


    <script src="./jquery-3.6.0.min.js"></script>
    <script src="./jquery.form.min.js"></script>
    <script>
        function uplpadfile() {
            //获取表单的数据        
            var formdata = new FormData(document.getElementById("myform"));
            //获取表单的数据        
            // var formdata
            // var file = $("#files").get(0);
            // var files = file.files;
            // var formdata = new FormData();
            // for (var i = 0; i < files.length; i++) {
            //     formdata.append("files", files[i]);
            // }
            // console.log(file);
            // console.log(files);
            // console.log(formdata);
            // console.log(formdata.get("files"));

            $.ajax({
                type: 'Post',
                data: formdata,
                contentType: false,
                processData: false,
                url: "http://localhost:5193/WeatherForecast/Upload/FormImg",

                success: function(result) {
                    console.log(result);
                    if (result.code == "1000") {
                        var imglist = result.saveFilePath;
                        document.getElementById("imglist").innerText = imglist;
                    } else {
                        alert('提交失败,重新尝试提交');
                    }
                }
            })
        };
    </script>
</body>

</html>